/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-10-20
 * Time: 20:38
 */
public class Main {

    /**
     * 二分查找
     * 给定一个 n 个元素有序的（升序）整型数组 nums 和一个目标值 target
     * 写一个函数搜索 nums 中的 target，如果目标值存在返回下标，否则返回 -1。
     * https://leetcode.cn/problems/binary-search/description/
     * @param nums
     * @param target
     * @return
     */
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1, ret = -1;
        while (left <= right) {
            // int mid = (left + right) / 2;
            int mid = left + (right - left) / 2;//防止溢出
            if (target > nums[mid]) {
                left = mid + 1;
            } else if (target < nums[mid]) {
                right = mid - 1;
            } else {
                ret = mid;
                break;
            }
        }
        return ret;
    }


    //暴力解法:

    /**
     * 在排序数组中查找元素的第一个和最后一个位置
     * https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/
     * @param nums
     * @param target
     * @return
     */
    public int[] searchRange1(int[] nums, int target) {
        int[] ret = new int[]{-1, -1};
        if (nums == null) {
            return ret;
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                ret[0] = i;
                break;
            }
        }
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] == target) {
                ret[1] = i;
                break;
            }
        }
        return ret;
    }


    public int[] searchRange(int[] nums, int target) {

        int[] ret = new int[2];
        ret[0] = -1;
        ret[1] = -1;
        if (nums.length == 0) {
            return ret;
        }
        //求左端点
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        //此时,left=right
        //判断
        if (nums[left] == target) {
            ret[0] = left;
        } else {
            //没找到
            return ret;
        }
        //求右端点
        left = 0;
        right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] > target) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        //此时,left=right
        if (nums[right] == target) {
            ret[1] = right;
        } else {
            return ret;
        }
        return ret;
    }


    /**
     * x的平方根
     * https://leetcode.cn/problems/sqrtx/description/
     * @param x
     * @return
     */
    public int mySqrt(int x) {
        long left = 0, right = x;
        while (left < right) {
            long mid = left + (right - left + 1) / 2;
            if (mid * mid <= x) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return (int) left;
    }

    /**
     * 搜索插入位置
     * https://leetcode.cn/problems/search-insert-position/
     * @param nums
     * @param target
     * @return
     */
    public int searchInsert(int[] nums, int target) {
        if (target > nums[nums.length - 1]) {
            return nums.length;
        }

        int left = 0;
        int right = nums.length - 1;

        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return left;
    }

    /**
     * 山脉数组的峰顶索引
     * https://leetcode.cn/problems/peak-index-in-a-mountain-array/description/
     * @param arr
     * @return
     */
    public int peakIndexInMountainArray(int[] arr) {
        int left = 1;
        int right = arr.length - 2;
        while (left < right) {
            int mid = left + (right - left + 1) / 2;
            if (arr[mid] >= arr[mid - 1]) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }


    /**
     * 寻找峰值
     * https://leetcode.cn/problems/find-peak-element/description/
     * @param nums
     * @return
     */
    //暴力解法
    public int findPeakElement(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        if (nums[0] > nums[1]) {
            return 0;
        }
        int cur = 0;
        while (cur < nums.length - 1) {
            if (nums[cur + 1] < nums[cur]) {
                return cur;
            }
            cur++;
        }
        return cur;
    }

    //二分查找
    public int findPeakElement1(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[mid + 1]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }


    /**
     * 寻找旋转数组中的最小值
     * https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/
     * @param nums
     * @return
     */
    public int findMin(int[] nums) {
        int left = 0;
        int n = nums.length-1;
        int right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left+1) / 2;
            if (nums[mid] > nums[mid + 1]) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        return nums[left];
    }

    /**
     * 缺失的数
     * https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/description/
     */
    public int takeAttendance(int[] records) {
        if (records.length == 1) {
            if (records[0] == 0) {
                return 1;
            }
            return records[0] - 1;
        }
        int left = 0;
        int right = records.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (mid == records[mid]) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if (records[left] == left) {
            return left + 1;
        }
        return left;
    }

}
